// pages/detail-song/detail-song.js
import {
  getRecomendList
} from "../../services/music"
import playerStore from "../../store/playerStore"
import rankingStore from "../../store/rankingStore"
import recomendStore from "../../store/recomendStore"

Page({
  data: {
    songsInfo: {},
    key: '',
    type: '',
    id: ''
  },
  onLoad(options) {
    // 1,确定获取数据的类型
    // type:ranking->榜单数据
    // type :recommend->推荐数据
    const type = options.type
    this.setData({
      type:type
    })
    // 从store中获取数据
    if (type === 'ranking') {
      const key = options.key
      this.setData({key})
      console.log(key);
      rankingStore.onState(key, this.handleRanking)
    } else if (type === "recommend") {
      recomendStore.onState("recomendSongInfo", this.handleRanking)
    } else if (type === "menu") {
      console.log(options.id);
      this.data.id = options.id
      this.fecthMenuSongInfo()
    }

  },
  async fecthMenuSongInfo() {
    const res = await getRecomendList(this.data.id)
    console.log(res);
    this.setData({
      songsInfo:res.playlist
    })
  },
  // ---点击事件
  onSongItemV2(){
playerStore.setState('playSongList',this.data.songsInfo.tracks)
  },
  handleRanking(value) {
    if (this.data.type === "recommend") {
      value.name = "推荐歌曲"
    }
    console.log(value);
    this.setData({
      songsInfo: value
    })
    wx.setNavigationBarTitle({
      title: value.name,
    })
  },
  onUnload() {
    if (this.data.type === 'ranking') {

      rankingStore.offState(this.data.key, this.handleRanking)
    } else if (this.data.type === "recommend") {
      recomendStore.offState("recomendSongInfo", this.handleRanking)

    }
  }
  // onLoad(options) {
  //   recomendStore.onState("recomendSongs", this.handleRecomendSongs)
  // },
  // handleRecomendSongs(value) {
  //   this.setData({
  //     songs: value
  //   })
  // },
  // onUnload() {
  //   recomendStore.offState("recomendSongs", this.handleRecomendSongs)
  // }
})